package com.dao.green.cn.test01.uis.adapters;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.dao.green.cn.test01.R;
import com.dao.green.cn.test01.beans.RecyclerTestBean;

import java.util.List;

/**
 * Created by Ant on 2017/8/28  10:32
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class RecyclerTestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<RecyclerTestBean> data;

    public void setData(List<RecyclerTestBean> data) {
        if (data == null)
            return;
        this.data = data;
        this.notifyDataSetChanged();
    }

    public RecyclerTestAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_ac_recyle_test_recycleview, parent, false);
        TestViewHolder testViewHolder = new TestViewHolder(root);
        testViewHolder.obt= (Button) root.findViewById(R.id.item_button);
        testViewHolder.obtHide= (Button) root.findViewById(R.id.item_button1);
        testViewHolder.obt.setTag(testViewHolder);

        testViewHolder.obt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 TestViewHolder hold= (TestViewHolder) v.getTag();
                Log.i("TAG","onclicked....");
                hold.obtHide.setVisibility(hold.obtHide.getVisibility()==View.GONE?View.VISIBLE:View.GONE);
            }
        });
        return testViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RecyclerTestBean bean = data.get(position);
        if (bean == null)
            return;
        TestViewHolder hold = (TestViewHolder) holder;
        hold.obt.setText(bean.getName());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }


    /**
     * holder
     */
    public class TestViewHolder extends RecyclerView.ViewHolder {
        private View root;
        private CardView cardView;
        private Button obt,obtHide;


        public TestViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            cardView = (CardView) root.findViewById(R.id.cardview);
            obt = (Button) root.findViewById(R.id.item_button);
        }
    }
}
